<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UploadController extends Controller
{
    public function upload(Request $request){
		$file = $request->file('file');
		//判断当前文件是否合法
		if ($file->isValid()){
			$path = $file->store(date('ymd'),'upload');
			//上传完成之后,需要按照手册中的方式给固定的返回值
			return ['file' => asset('/uploadImages/' . $path), 'code' => 0];
		}
	}

    /**
     * 为编辑器上传图片处理
     * @param Request $request
     * @return array
     */
	public function bianjiqiUpload(Request $request){
		//获取前台请求过来的数据
		$file = $request->file('file');
		if ($file->isValid()){
			$path = $file->store(date('ymd'),'upload');
			//上传完毕,应该按照手册给对应的返回值
			return [
				"success"   => true,
				"msg"       => "图片上传成功",
				"file_path" => asset('/uploadImages/' . $path)//文件地址
			];
		}
	}

	/**
     * 获取已经上传的图片数据
     */

	public function fileLists(){
        $files = glob('uploadImages/*/*');
        foreach ($files as $f) {
            $file   = "/".$f;
            $data[] = [
                'url'        => $file,
                'path'       => $file,
                'size'       => filesize($f),
                'name'       => basename($f),
                'createtime' => date('Y-m-d', filemtime($f)),
            ];
        }

        return ['data' => $data,'code'=>0, 'page' => []];
    }
}
